package com.zh.service.impl;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.constants.Constants;
import com.zh.constants.ErrorCode;
import com.zh.pojo.domain.Customer;
import com.zh.mapper.CustomerMapper;
import com.zh.pojo.dto.WxMinAppRegisterDto;
import com.zh.remote.api.LoginApi;
import com.zh.remote.pojo.param.CreateLoginDto;
import com.zh.result.JSONResult;
import com.zh.service.ICustomerFineService;
import com.zh.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.service.ICustomerSummaryService;
import com.zh.service.ICustomerWalletService;
import com.zh.template.WeChatTemplate;
import com.zh.utils.AssertUtil;
import com.zh.utils.BitStatesUtils;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author 初四
 * @since 2024-06-13
 */
@Service
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private WeChatTemplate weChatTemplate;
    @Autowired
    private ICustomerWalletService walletService;
    @Autowired
    private ICustomerSummaryService summaryService;
    @Autowired
    private LoginApi loginApi;

    @Override
    // 开启全局事务,只在在入口方法贴注解 即可
    @GlobalTransactional(name = "Customer-wxregister",rollbackFor = Exception.class)
    public void wxregister(WxMinAppRegisterDto wxRegisterDto) {
        // 1、获取OpenId
        String openId = weChatTemplate.getOpenId(wxRegisterDto.getCode());
        // 2. 根据OpenId 查询数据库，判断客户是否注册
        Customer customer = selectCustomer(openId);
        // 3、保存乘客信息
        customer = saveCustomer(openId);
        // 4、保存乘客相关信息
        saveCustomerAssociation(customer);
        // 5、 远程调用 uaa 保存login信息
        createLogin(customer);
    }

    /**
     * 保存乘客相关信息
     *
     * @param customer
     */
    private void saveCustomerAssociation(Customer customer) {
        // 保存乘客的钱包
        walletService.saveCustomerWallet(customer);
        // 保存乘客的数据汇总
        summaryService.saveCustomerSummary(customer);
    }

    /**
     * 远程调用认证中心
     *
     * @param customer
     */
    private void createLogin(Customer customer) {
        CreateLoginDto loginDto = new CreateLoginDto();
        loginDto.setId(customer.getId());
        loginDto.setUsername(customer.getPhone());
        loginDto.setType(Constants.Login.TYPE_CUSTOMER);
        loginDto.setOpenId(customer.getOpenId());
        loginDto.setPhone(customer.getPhone());
        // 远程调用feign 接口
        JSONResult login = loginApi.createLogin(loginDto);
        AssertUtil.isTrue(login.isSuccess(), ErrorCode.SERVICE_REQUEST_ERROR.getMessage());

    }

    /**
     * // 保存 乘客信息
     *
     * @param openId
     * @return
     */
    private Customer saveCustomer(String openId) {
        Customer customer = new Customer();
        customer.setPhone("18381364990");
        customer.setCreateTime(new Date());
        customer.setOpenId(openId);
        // 给乘客添加手机认证的位状态
        customer.setBitState(BitStatesUtils.addState(0L, BitStatesUtils.OP_PHONE));
        customer.setLevel(Constants.Level.LEVEL_ZERO);
        super.save(customer);
        return customer;
    }

    /**
     * 根据OpenId 查询数据库，并判断客户是否注册
     *
     * @param openId
     * @return
     */
    private Customer selectCustomer(String openId) {

        Customer customer = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openId));
        // 打印日志
        log.info("Customer :{}", customer);
        // 断言判断
        AssertUtil.isNull(customer, ErrorCode.PARAM_PHONE_EXIST.getMessage());
        return customer;
    }
}
